import { cloneDeep } from "lodash-es";
import { IMenuType } from "./../router/index";
import { mainRoutes } from "@/router";

export const test = () => {
  console.log("test");
};

//将路由数据包整合为对象
// {'/dashboard':'数据统计'}
interface RouteMapType {
  [key: string]: string;
}
export const mapRoutesTool = () => {
  let routeMap: RouteMapType = {};
  function loop(arr: IMenuType[]) {
    arr.forEach((item) => {
      routeMap[item.key] = item.title;
      if (item.children) {
        loop(item.children);
      }
    });
  }
  loop(mainRoutes);
  return routeMap;
};

//根据角色权限数组permission，筛选菜单数据
// lodash 工具函数库
export const menuFilter = (permission: Array<string>) => {
  // let arr = []
  let routeArr = cloneDeep(mainRoutes);
  function loop(arr: Array<IMenuType>) {
    for (let i = arr.length - 1; i >= 0; i--) {
      let bool = permission.includes(arr[i].key); //找相同
      let likeArr = permission.filter((path) => path.indexOf(arr[i].key) != -1); //看permission中有没有跟当前路径相似的路径
      if (!bool && likeArr.length == 0) {
        arr.splice(i, 1);
      }
      if ((bool || likeArr.length) && arr[i].children) {
        loop(arr[i].children!);
      }
    }
  }
  loop(routeArr);
  console.log("处理权限后的菜单数据包", routeArr);
  return routeArr;
};
